package lib.quasar.widget.image;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import lib.quasar.util.DimenUtil;
import lib.quasar.widget.R;

/**
 * description: 头像
 * created by kalu on 2018/3/26 13:46
 */
public class CircleImageView extends AppCompatImageView {

    /*******************************************************************************/

    private final Paint mPaint = new Paint();
    private int circleColor = Color.GREEN;

    /*******************************************************************************/

    public CircleImageView(Context context) {
        this(context, null, 0);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = null;
        try {

            Context applicationContext = context.getApplicationContext();
            array = applicationContext.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
            circleColor = array.getColor(R.styleable.CircleImageView_civ_circle_color, circleColor);

        } catch (Exception e) {

        } finally {
            if (null != array) {
                array.recycle();
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(circleColor);
        float width = DimenUtil.dp2px(2);
        mPaint.setStrokeWidth(width);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - width / 2, mPaint);
    }

    public void setCircleColor(int mCircleColor) {
        this.circleColor = mCircleColor;
        postInvalidate();
    }

    public boolean isChecked() {
        return circleColor == Color.parseColor("#13ad67");
    }
}
